package hibernateDAOS;

import hibernate.HibernateUtil;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;


import beans.Rubro;

public class RubroDAO {
	
	private static RubroDAO instancia = null;
	private static SessionFactory sf = null;

	public static RubroDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new RubroDAO();
		} 
		return instancia;
	}
	
	public void guardarRubro(Rubro r){
		Session session = sf.openSession();
		session.beginTransaction();
		session.save(r);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void eliminarRubro(Rubro r){
		Session session = sf.openSession();
		session.beginTransaction();
		session.delete(r);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void modificarRubro(Rubro r){
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(r);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	@SuppressWarnings("unchecked")
	public List<Rubro> obtenerRubros(){
		Session session = sf.openSession();
		Query q = session.createQuery("from Rubros");
		List<Rubro> rubros = q.list();
		session.close();
		return rubros;
	}
}


